package campshop.daos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import campshop.models.GoodsInfo;
public class GoodsInfoDao extends JdbcDaoSupport{

	public long addGoodsInfo(GoodsInfo gi)
	{
		String sql = "insert into " +
				"tbl_goods_info (gi_sGoodsName, si_uShopId, gi_dCreateDate, gi_sDesInfo, gi_sImagePath1, gi_sImagePath2," +
				"gi_sImagePath3, gi_sUnitsOfMeasurement, gi_bIsOnSell, gi_uGoodsNum, gi_fMoney)" +
				" values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		if(super.getJdbcTemplate().update(sql, gi.getsGoodsName(), gi.getuShopId(), gi.getdCreateDate(), gi.getsDesInfo(),
				gi.getsImagePath1(), gi.getsImagePath2(), gi.getsImagePath3(), gi.getsUnitsOfMeasurement(),
				gi.isbIsOnSell()?1:0, gi.getuGoosNum(), gi.getfMoney())>0){
			return super.getJdbcTemplate().queryForInt("select max(gi_uGoodsId) from tbl_goods_info");
		}else return 0L;
	}
	
	public boolean delGoodsInfo(long uGoodsId)
	{
		String sql = "delete from tbl_goods_info where gi_uGoodsId = ?";
		if(super.getJdbcTemplate().update(sql, uGoodsId)>0) return true;
		return false;
	}
	
	public boolean updateGoodsInfo(GoodsInfo gi) {
		String sql="update tbl_goods_info " +
				"gi_sGoodsName= ?, si_uShopId= ?, gi_dCreateDate= ?, gi_sDesInfo= ?, gi_sImagePath1= ?, gi_sImagePath2= ?," +
				"gi_sImagePath3= ?, gi_sUnitsOfMeasurement= ?, gi_bIsOnSell= ?, gi_uGoodsNum= ?, gi_fMoney= ?" +
				"where gi_uGoodsId = ?";
		if(super.getJdbcTemplate().update(sql, gi.getsGoodsName(), gi.getuShopId(), gi.getdCreateDate(), gi.getsDesInfo(),
				gi.getsImagePath1(), gi.getsImagePath2(), gi.getsImagePath3(), gi.getsUnitsOfMeasurement(),
				gi.isbIsOnSell()?1:0, gi.getuGoosNum(), gi.getfMoney(), gi.getuGoodsId())>0){
			return true;
		}else return false;
	}
	
	public GoodsInfo getGoodsInfo(long uGoodsId)
	{
		String sql = "select * from tbl_goods_info where gi_uGoodsId=" + uGoodsId;
		return (GoodsInfo)super.getJdbcTemplate().queryForObject(sql, new GoodsInfoRowMapper());
	}
	
	public List<GoodsInfo> getGoodsInfoListByShopId(long uShopId)
	{
		String sql = "select * from tbl_goods_info where si_uShopId=" + uShopId + "order by gi_dCreateDate asc";
		return super.getJdbcTemplate().query(sql, new GoodsInfoRowMapper());
	}
	class GoodsInfoRowMapper implements RowMapper<GoodsInfo>{
		@Override
		public GoodsInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
			GoodsInfo gi = new GoodsInfo();
			gi.setuGoodsId(rs.getLong("gi_uGoodsId"));
			gi.setsGoodsName(rs.getString("gi_sGoodsName"));
			gi.setuShopId(rs.getLong("si_uShopId"));
			gi.setdCreateDate(rs.getDate("gi_dCreateDate"));
			gi.setsDesInfo(rs.getString("gi_sDesInfo"));
			gi.setsImagePath1(rs.getString("gi_sImagePath1"));
			gi.setsImagePath2(rs.getString("gi_sImagePath2"));
			gi.setsImagePath3(rs.getString("gi_sImagePath3"));
			gi.setsUnitsOfMeasurement(rs.getString("gi_sUnitsOfMeasurement"));
			gi.setbIsOnSell(rs.getBoolean("gi_bIsOnSell"));
			gi.setuGoosNum(rs.getLong("gi_uGoodsNum"));
			gi.setfMoney(rs.getFloat("gi_fMoney"));
			return gi;
		}	
	}
}